<template>
  <div class="login">
    <el-card>
      <img src="@/assets/logo.png" alt="" class="logo" />
      <el-form :model="ruleForm" :rules="rules" ref="form">
        <el-form-item prop="mobile">
          <el-input
            v-model="ruleForm.mobile"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="ruleForm.code"
            placeholder="请输入验证码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="ruleForm.isPass" checked>
            我已阅读并同意「用户协议」和「隐私条款」
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { authorizations } from '@/api/index.js'
import { setToken } from '@/utils/auth.js'
export default {
  name: 'Login',
  data () {
    return {
      ruleForm: {
        mobile: '13200000000',
        code: '246810',
        isPass: []
      },
      rules: {
        mobile: [
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号'))
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { len: 6, message: '请输入6位验证码', trigger: 'blur' }
        ],
        isPass: [
          {
            required: true,
            message: '请勾选我已阅读并同意「用户协议」和「隐私条款」',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          const res = await authorizations(this.ruleForm)
          setToken(res.data.token)
          this.$message.success('登录成功')
          this.$router.push('/home')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background: url(~@/assets/login.png);
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;

  .el-card {
    width: 440px;

    .logo {
      width: 200px;
      display: block;
      margin: 0 auto 20px;
    }

    .el-form {
      padding: 0 20px;

      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
